<?php
namespace Home\Controller;

class UserController extends HomeController {
    //主页列表
    public function index(){
        //查部门,在搜索里显示
        $arr = M('group')->select();
        $group = [];     
        foreach ($arr as $k => $v) {
            $group[$v['id']] = $v['groupname'];
        }

        //搜索分页显示一起
        $m=M('user');

        //搜索
        $keywordN = '%'.$_GET['n'].'%';  //获取搜索关键字 
        $where['name'] = array('like',$keywordN);

        if($_GET['g']){
            $keywordG = $_GET['g'];  //获取搜索关键字 
            $where['groupid'] = array('eq',$keywordG);    
        }

        //分页 这个函数在app下的com下com里
        $p=getpage($m,$where,7);
        $lista=$m->field(true)->where($where)->select();
            
        
        foreach ($lista as $k => $v) {
            //把部门添加到数组中
            $gid = $v['groupid'];
            $infog = M('group')->field('groupname')->where(array('id'=>array('eq',$gid)))->select();
            $lista[$k]['gid'] = $infog[0]; 
            //把职位添加到数组
            $pid = $v['uposition'];
            $infop = M('roleposition')->field('remark')->where(array('position'=>array('eq',$pid)))->select();
            $lista[$k]['pid'] = $infop[0]; 
        }

          
        //dump($lista);exit;
        $this->assign('name1',$name1['name']);
        $this->assign('group',$group);
        $this->assign('lista',$lista);
        $this->page=$p->show();
        $this->display();
    }

    //详情页面
    public function detail(){
       //接收传来的ID
        $id = I('get.id');
        $list = M('userdetail')->where(array('uid'=>array('eq',$id)))->select();
        //dump($list);exit;
        $this->assign('name1',$name1['name']);
        $this->assign('list',$list);
        $this->display();
    }

    //添加页面
    public function add(){
        //查部门
        $arr = M('group')->select();
        $group = [];
        
        foreach ($arr as $k => $v) {
            $group[$v['id']] = $v['groupname'];
        }

        $this->assign('name1',$name1['name']);
        $this->assign(group,$group);
        $this->display();
    }

    //执行添加
    public function insert(){
            if(!D('User')->create()){
                $this->error(D('User')->getError());
                exit;
            }

            if(D('User')->add() > 0){

                //同时生成用户详情表
                $detail['uid'] = D('User')->getLastInsId();
                $detail['uname'] = $_POST['name'];
                $detail['headpic'] = "default.jpg";
                M('userdetail')->create($detail);
                M('userdetail')->add();

                //并且同时生成username用于登录
                $user['password'] = '000000'; //默认密码6个0
                $user['username']  = $detail['uname'].$detail['uid'];
                M('user')->create($user);
                M('user')->where(array('id'=> $detail['uid']))->save();

                $this->success("添加成功！",U('User/index'));
            }else{
                $this->error("添加失败！");
            }

    }
}